#ifndef BUFFER_H
#define BUFFER_H

#include "types.h"
#include "math_helpr.h" 

NS_BEGIN(sk)

class buffer {
public:
    static const size_t DEFAULT_BUFFER_SIZE = 512;
    MAKE_NONCOPYABLE(buffer);

    buffer(size_t buffer_size = DEFAULT_BUFFER_SIZE);
    buffer(void *data, size_t len);
    ~buffer();

    void commit(size_t n);
    void consume(size_t n);
    void *prepare(size_t n);

    const void *peek() const {
        return sk::byte_offset<void>(address, rindex_);
    }

    void *mutable_peek() const {
        return sk::byte_offset<void>(address, rindex_);
    }

    size_t size() const {
        assert(windex_ >= rindex_);
        return windex_ - rindex_;
    }

    bool empty() const {
        assert(windex_ >= rindex_);
        return windex_ == rindex_;
    }

private:
    void ensure_space(size_t len);

private:
    void *address_;
    size_t capacity_;
    size_t windex_;
    size_t rindex_;
};

NS_END(sk)

#endif // BUFFER_H 
